// pages/site/site.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(app.globalData.userInfo != null){
      var userInfo = app.globalData.userInfo;
      this.setData({
        'userInfo':userInfo,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onChooseAvatar(e) {
    console.log(e.detail)
    this.setData({
     "userInfo.head" :e.detail.avatarUrl
    })
  },
  name:function(e){
    this.setData({
      "userInfo.name": e.detail.value
    })
        
  },
  repair(){
    var that = this
    var src = that.data.userInfo.head
    if(src.indexOf("/upload/xcx/") == -1 && src != 'https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132'){
      app.upload(that,src,that.edit);
    }else{
      that.edit(that)
    }
    
  },
  edit(that){
  
    var postData = {
      name:that.data.userInfo.name,
    }
    if('autograph' in that.data){
      postData.head = that.data.autograph
    }
    app.call.request('user_check/editUser',postData, function(data){
      app.showTip('修改成功');
      if('autograph' in that.data){
        app.globalData.userInfo.head = app.globalData.domain+that.data.autograph
      }
      app.globalData.userInfo.name = that.data.userInfo.name
      wx.navigateBack({
        delta: 1
      });
    },function(){});
  }

  
})